package org.infodavid.common.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.infodavid.common.IBuilder;
import org.infodavid.common.ejb.DocumentVersionBean;
import org.infodavid.common.model.behaviors.IApprovable;

/**
 * The Class DocumentTest.
 */
public class DocumentVersionTest extends AbstractBeanTest<IDocumentVersion> {

	/** The user test. */
	private final UserTest userTest = new UserTest(this);

	/** The document test. */
	private final DocumentTest documentTest = new DocumentTest(this);

	/** The category test. */
	private final DocumentCategoryTest categoryTest = new DocumentCategoryTest(this);

	/**
	 * Instantiates a new document version test.
	 */
	public DocumentVersionTest() {
		super();
	}

	/**
	 * Instantiates a new document version test.
	 * @param mainTest the main test
	 */
	public DocumentVersionTest(final AbstractPersistenceTest mainTest) {
		super(mainTest);
	}

	/**
	 * Populate.
	 * @param count the count
	 * @return the collection< test data>
	 * @throws Exception the exception
	 */
	@Override
	protected List<IDocumentVersion> doCreate(final int count) throws Exception {
		final IBuilder<IDocumentVersion> builder =
		    ModelFactory.getInstance().getBuilder(IDocumentVersion.class);
		final Iterator<IUser> users = userTest.populate(count).iterator();
		final List<IDocumentVersion> items = new ArrayList<IDocumentVersion>(count);
		final List<IDocument> documents = documentTest.populate(count + 1);
		final IDocumentCategory category = categoryTest.populate(1).get(0);
		IDocumentVersion item;
		long key;

		for (int i = 0; i < count; i++) {
			item = builder.create();
			key = System.nanoTime();

			item.setDocument(documents.get(i + 1));
			item.setArchived(false);
			item.setName("item-" + key);
			item.setApprovalStatus(IApprovable.EApprovalStatus.values()[RAND
			    .nextInt(IApprovable.EApprovalStatus.values().length)]);
			item.setCategory(category);
			item.setComment("comment-" + key);
			item.setData(item.getComment().getBytes());
			item.setEncoding("UTF-8");
			item.setLanguage(Locale.getISOLanguages()[RAND.nextInt(Locale.getISOLanguages().length)]);
			item.setTitle("title-" + key);
			item.setRevision(i + 1);

			item.getProperties().addData("key1", "value1");
			item.getProperties().addData("key2", "value2");

			if (!items.isEmpty()) {
				item.addAttachment(documents.get(0));
			}

			item.setUser(users.next());

			items.add(item);
		}

		return items;
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.AbstractBeanTest#getEntityClass()
	 */
	@Override
	public Class<DocumentVersionBean> getEntityClass() {
		return DocumentVersionBean.class;
	}

	/**
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.AbstractBeanTest#getEntityName()
	 */
	@Override
	public String getEntityName() {
		return IDocumentVersion.class.getSimpleName();
	}
}
